#!/usr/bin/env bash
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
source "$PLUGIN_AVAILABLE_PATH/storage/functions"
source "$PLUGIN_AVAILABLE_PATH/docker-options/functions"

cmd-storage-unmount() {
  declare desc="Remove bind-mount, restart app if running"
  declare cmd="storage:unmount"
  [[ "$1" == "$cmd" ]] && shift 1
  declare APP="$1" MOUNT_PATH="$2"
  local passed_phases=(deploy run)

  [[ -z "$APP" ]] && dokku_log_fail "Please specify an app to run the command on"
  [[ -z "$MOUNT_PATH" ]] && dokku_log_fail "Please specify two paths delimited by colon."
  verify_app_name "$APP"
  verify_paths "$MOUNT_PATH"
  check_if_path_exists "$MOUNT_PATH" "$(get_phase_file_path "${passed_phases[@]}")" || dokku_log_fail "Mount path does not exist."
  remove_passed_docker_option passed_phases[@] "-v $MOUNT_PATH"
}

cmd-storage-unmount "$@"
